package com.zdlee.netty.server6;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.nio.charset.Charset;
import java.util.Date;

/**
 * @author zdlee
 * @description: ${DESCRIPTION}
 * @create 2018-11-27 15:39
 */
public class FirstClientHandler extends ChannelInboundHandlerAdapter {


    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(new Date()+": 客户端写出数据");
        //获取数据
        ByteBuf buffer=getByteBuf(ctx);
        //写数据
        ctx.channel().writeAndFlush(buffer);

    }

    private ByteBuf getByteBuf(ChannelHandlerContext ctx) {
        byte[] bytes="你好,闪电侠!".getBytes(Charset.forName("utf-8"));
        ByteBuf byteBuf=ctx.alloc().buffer();
        byteBuf.writeBytes(bytes);
        return byteBuf;
    }


}
